<?php
/**
 * Description:
 *
 * @author      Eden
 * @datetime    2017/2/8 21:47
 * @copyright   WuHan HuQi Technology Co.,Ltd.
 * @url         http://cnhuqi.com
 */

class Pay_Model extends CI_Model
{
    /**
     * 初始化
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 保存修改预支付信息
     * @param array $data
     * @param string $sn
     */
    public function saveWxBuy(Array $data, $sn = '')
    {

        try {
            if (!empty($sn)) {
                $this->db->where('out_trade_no', $sn);
                $this->db->update('user_wx_buy', $data);
                return $this->db->affected_rows();
            } else {

                $db = clone($this->db);
                $query = $db->get_where('user_wx_buy', ['out_trade_no' => $data['sn']]);
                $row = $query->row_array();
                if (!empty($row) && $row['status'] != 6) {
                    $db->where(['out_trade_no' => $data['sn']]);
                    $db->delete('user_wx_buy');
                }

                $this->db->insert('user_wx_buy', [
                    'out_trade_no' => value($data, 'sn', ''),
                    'userid' => value($data, 'userid', 0),
                    'openid' => value($data, 'openid', ''),
                    'total_fee' => value($data, 'total_fee', 0),
                    'prepay_id' => value($data, 'prepay_id', ''),
                    'transaction_id' => value($data, 'transaction_id', ''),
                    'status' => value($data, 'status', 0),
                ]);

                return $this->db->insert_id();
            }
        } catch(Exception $e) {}
    }

    /**
     * 客户端下单
     * @param array $data
     * @param string $sn
     * @return mixed
     */
    public function saveWxAppBuy(Array $data, $sn = '')
    {
        $insertData = array (
            'userid' => value($data, 'userid', 0),
            'appid' => value($data, 'appid', ''),
            'bank_type' => value($data, 'bank_type', ''),
            'cash_fee' => value($data, 'cash_fee', 0),
            'fee_type' => value($data, 'fee_type', 'CNY'),
            'is_subscribe' => value($data, 'is_subscribe', 'N'),
            'mch_id' => value($data, 'mch_id', ''),
            'nonce_str' => value($data, 'nonce_str', ''),
            'openid' => value($data, 'openid', ''),
            'out_trade_no' => value($data, 'out_trade_no', ''),
            'time_end' => value($data, 'time_end', 0),
            'total_fee' => value($data, 'total_fee', 0),
            'trade_type' => 'APP',
            'transaction_id' => value($data, 'transaction_id', ''),
            'prepayid' => value($data, 'prepayid', ''),
            'status' => value($data, 'status', 0),
            'error_msg' => value($data, 'error_msg', ''),
            'error_code' => value($data, 'error_code', '')
        );

        $db = clone($this->db);
        if (empty($sn)) {
            // 添加
            $db->insert('user_order_wechat', $insertData);
            return $db->insert_id();
        } else {
            // 修改
            foreach($data as $key => $val) {
                if (!array_key_exists($key, $insertData)) {
                    unset($data[$key]);
                }
            }
            $db->where('out_trade_no', $sn);
            $db->update('user_order_wechat', $data);
            return $db->affected_rows();
        }
    }
}
